package com.alpha.processor.creators.binder_file;

import com.alpha.processor.creators.info_model.ConnectorInterfaceInfoModel;
import com.alpha.processor.processor.tools.ProcessorTool;
import com.alpha.processor.creators.base.CreateModel;
import com.alpha.processor.creators.connector_file.ConnecteCreateModel;
import com.alpha.processor.creators.connector_file.ConnectorFileCreator;

import java.util.HashMap;
import java.util.Map;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.Elements;

public class BinderCreateModel extends CreateModel {

    public static final String DataBinder = "VPBinder";
    public static final String ParamName = "Manager";
    public static final String MethodName = "findConnector";

    private final Map<String,String> generateClassMap = new HashMap<>();

    private final Map<Integer,String> viewIdMap = new HashMap<>();

    private ConnectorInterfaceInfoModel connectorInfoModel;

    public BinderCreateModel(ProcessorTool logger, Elements elementUtils, TypeElement classElement) {
        super(logger, elementUtils, classElement);
    }

    public void updateConnectorInterfaceInfoModel(ConnectorInterfaceInfoModel connectorInfoModel){
        this.connectorInfoModel = connectorInfoModel;
    }

    public void syncRClassElement(Element rClassElement){
        final String idElementStr = "id";
        for(Element element:rClassElement.getEnclosedElements()) {
            if (idElementStr.equals(element.getSimpleName().toString())){
                // R.id.*
                for(Element idElement:element.getEnclosedElements()) {
                    if (idElement.getKind() == ElementKind.FIELD){
                        VariableElement fieldElement = (VariableElement) idElement;
                        Integer id = (Integer) fieldElement.getConstantValue();
                        String name = fieldElement.getSimpleName().toString();
                        viewIdMap.put(id,name);
                    }
                }
            }
        }
    }

    public void syncGenerateClassMap(Map<String, ConnectorFileCreator> connectorCreatorMap){
        for (String targetClassName:connectorCreatorMap.keySet()){
            if (targetClassName == null){
                continue;
            }
            ConnecteCreateModel connecteCreateModel = connectorCreatorMap.get(targetClassName).getCreateModel();
            connecteCreateModel.getViewInfoModel().updateViewIdMap(viewIdMap);
            String generateFileName = connecteCreateModel.getGenerateFileFullName();
            if (generateFileName == null){
                continue;
            }
            generateClassMap.put(targetClassName,generateFileName);
        }
    }

    @Override
    public String getGenarateFileName() {
        return DataBinder;
    }

    public Map<String, String> getGenerateClassMap() {
        return generateClassMap;
    }

    public ConnectorInterfaceInfoModel getConnectorInfoModel() {
        return connectorInfoModel;
    }
}
